package boot3.service.impl;

import boot3.mapper.ApplicationMapper;
import boot3.mapper.ProjectMapper;
import boot3.pojo.Application;
import boot3.pojo.Project;
import boot3.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private ApplicationMapper applicationMapper;

    @Override
    public List<Project> findProjectByCollege(String college) {
        return projectMapper.findProjectByCollege(college);
    }

    @Override
    public List<Project> findAllProject() {
        return projectMapper.findAllProject();
    }

    @Override
    public List<Project> findProjectByTeacher(String teacher_id) {
        return projectMapper.findProjectByTeacher(teacher_id);
    }

    @Override
    public Boolean addProject(Project project) {
        //先获取最后一个科创项目的index
        int lastProjectIndex=projectMapper.findAllProject().size()-1;
        int lastProjectId= Integer.parseInt(projectMapper.findAllProject().get(lastProjectIndex).getProject_id());
        project.setProject_id(String.valueOf(lastProjectId+1));
        return projectMapper.addProject(project);
    }

    @Override
    public Boolean deleteProject(String project_id) {
        //先获取这个大创项目的申请，查看是否已经有学生参加
        List<Application> applications = applicationMapper.selectApplicationByProjectId(project_id);
        if(!applications.isEmpty()){
            return applicationMapper.updateApplicationStateOver(project_id) && projectMapper.deleteProject(project_id);
        }
        else{
            return projectMapper.deleteProject(project_id);
        }
    }

    @Override
    public Boolean setProjectNotShow(String project_id) {
        return projectMapper.setProjectNotShow(project_id);
    }
}
